package com.seven.ezpermissions.sample

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.seven.ezpermissions.AppSettingsDialog
import com.seven.ezpermissions.EzPermissions
import com.seven.ezpermissions.PermissionRequest
import kotlinx.android.synthetic.main.fragment_main.*

/**
 * fragment sample
 * @author seven.xie
 * @date 2020/7/13 15:06
 * email: se7en.xie@qq.com
 */
class MainFragment : Fragment() {

    companion object {
        private const val PERMISSIONS_REQUEST_CODE_PHONE_CALL = 1001
        private const val PERMISSIONS_REQUEST_CODE_CAMERA = 1002
        private const val PERMISSIONS_REQUEST_CODE_FILE_STORAGE = 1003
        private const val PERMISSIONS_REQUEST_CODE_LOCATION = 1004
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btnRequestPhoneCall.setOnClickListener {
            requestPhoneCallPermissions()
        }

        btnRequestCamera.setOnClickListener {
            requestCameraPermissions()
        }

        btnRequestFileStorage.setOnClickListener {
            requestFileStoragePermissions()
        }

        btnRequestLocation.setOnClickListener {
            requestLocationPermissions()
        }

        btnNext.setOnClickListener {
            startActivity(Intent(context, NothingActivity::class.java))
        }

        btnFinishNext.setOnClickListener {
            startActivity(Intent(context, NothingActivity::class.java))
            activity?.finish()
        }
    }

    private fun requestPhoneCallPermissions() {
        EzPermissions.request(
            PermissionRequest.Builder(
                this,
                arrayOf(Manifest.permission.CALL_PHONE),
                PERMISSIONS_REQUEST_CODE_PHONE_CALL
            ).setRationale("拨号功能需要您授予 拨号 权限，否则无法拨号。")
                .setPositiveButton(R.string.agree)
                .setNegativeButton(R.string.ez_permissions_cancel)
                .onGranted {
                    toast("Dial permission has been granted!")
                }
                .onDenied { _, _ ->
                    toast("Dial permission has been denied!")
                    AppSettingsDialog.Builder(this)
                        .setTitle(R.string.app_settings_dialog_title_dial)
                        .setRationale(R.string.app_settings_dialog_msg_dial)
                        .setNegativeButton("取消")
                        .setPositiveButton(R.string.to_settings_ui)
                        .show()
                }
                .build()
        )
    }

    private fun requestCameraPermissions() {
        EzPermissions.request(
            PermissionRequest.Builder(
                this,
                arrayOf(Manifest.permission.CAMERA),
                PERMISSIONS_REQUEST_CODE_CAMERA
            ).setRationale("此功能需要您授予 相机 权限，否则无法正常使用。")
                .setPositiveButton("接受")
                .setNegativeButton("拒绝")
                .onGranted {
                    toast("Camera permission has been granted!")
                }
                .onDenied { _, _ ->
                    toast("Camera permission has been denied!")
                    AppSettingsDialog.Builder(this)
                        .setTitle("相机权限请求")
                        .setRationale("由于您拒绝授予 相机 权限，此功能将暂时禁用。如需开启，请打开App设置页面，手动授权。")
                        .setNegativeButton("忽略")
                        .setPositiveButton("去设置")
                        .show()
                }
                .build()
        )
    }

    private fun requestFileStoragePermissions() {
        EzPermissions.request(
            PermissionRequest.Builder(
                this,
                arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ),
                PERMISSIONS_REQUEST_CODE_FILE_STORAGE

            ).setRationale(R.string.rationale_file_storage)
                .onGranted {
                    toast("File storage permissions has been granted!")
                }
                .onDenied { _, _ ->
                    toast("File storage permissions has been denied!")
                    AppSettingsDialog.Builder(this)
                        .setRationale(R.string.app_settings_dialog_msg_file_storage)
                        .show()
                }
                .build()
        )
    }

    private fun requestLocationPermissions() {
        EzPermissions.request(
            this,
            arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ),
            PERMISSIONS_REQUEST_CODE_LOCATION,
            onGranted = {
                toast("Location permission has been granted!")
            },
            onDenied = { _, _ ->
                toast("Location permission has been denied!")
                AppSettingsDialog.Builder(this)
                    .setTitle(R.string.app_settings_dialog_title_location)
                    .setRationale(R.string.app_settings_dialog_msg_location)
                    .setNegativeButton(R.string.ignore)
                    .setPositiveButton(R.string.to_settings_ui)
                    .show()
            }
        )
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EzPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    private fun toast(text: String) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
    }
}